﻿using Microsoft.EntityFrameworkCore;
using RescueApp.Data;
using RescueApp.Models;
using RescueApp.Repository;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Rescue.Services
{
    public class RescueApplyService : Rescue1
    {
        private RescueRepos _repos;
        private RescueContext db;
        public RescueApplyService(RescueContext _db, RescueRepos repos)
        {
            _repos = repos;
            _repos.DbContext = _db;
            db = _db;
        }

        //查询 RescueApply 表中的所有记录，并返回一个 List<RescueApply>。
        public List<RescueApply> QueryAllItems()
        {
            return _repos.FindList<RescueApply>().ToList();
        }

        //根据 Id 查询单个 RescueApply 记录。
        public RescueApply FindById(int id)
        {
            return _repos.FindEntity<RescueApply>(s => s.RescueID == id);
        }

        public int Insert(RescueApply item)
        {
            return _repos.Insert(item);
        }
        public int Update(RescueApply item)
        {
            return _repos.Update(item);
        }
        public int Delete(RescueApply item)
        {
            return _repos.Delete(item);
        }

        public int? GetRescueIDByPhone(string phone)
        {
            var rescueApply = db.RescueApplies.FirstOrDefault(r => r.Phone == phone);
            return rescueApply?.RescueID;
        }
        public List<RescueApply> QueryUnassignedItems()//未派遣
        {
            return db.RescueApplies
                .Where(a => !db.RescueDispatches.Any(d => d.RescueID == a.RescueID))
                .ToList();
        }

        public List<RescueApply> QueryAssignedItems()//已派遣
        {
            return db.RescueApplies
                .Where(a => db.RescueDispatches.Any(d => d.RescueID == a.RescueID))
                .ToList();
        }
    }


    public class RescueDispatchService : Rescue2
    {
        private RescueRepos _repos;
        private RescueContext db;
        public RescueDispatchService(RescueContext _db, RescueRepos repos)
        {
            _repos = repos;
            _repos.DbContext = _db;
            db = _db;

        }

        
        public List<RescueDispatch> QueryAllItems()
        {
            return _repos.FindList<RescueDispatch>().ToList();
        }

      
        public RescueDispatch FindById(int id)
        {
            return _repos.FindEntity<RescueDispatch>(s => s.RescueID == id);//这里留个疑虑就是查询单个id记录是查派遣单号还是救援单号
        }

        public int Insert(RescueDispatch item)
        {
            return _repos.Insert(item);
        }
        public int Update(RescueDispatch item)
        {
            return _repos.Update(item);
        }
        public int Delete(RescueDispatch item)
        {
            return _repos.Delete(item);
        }
        public RescueDispatchStats GetStatistics(DateTime startDate, DateTime endDate)//返回某个时间段的统计值
        {
            var dispatches = db.RescueDispatches
                .Where(d => d.ArrivedTime >= startDate && d.ArrivedTime <= endDate)
                .ToList();

            return new RescueDispatchStats
            {
                TotalRescues = dispatches.Count,
                TotalCost = dispatches.Sum(d => d.Cost),

            };
        }

    }
    public class RescueDispatchStats
    {
        public int TotalRescues { get; set; }
        public float TotalCost { get; set; }
        
    }
}
